From 5668e8f33ec75484eaf874a215d668fbc7f8ee02 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 21 Dec 2016 09:22:00 +0300 Subject: [PATCH] Make sure targets always use absolute path --- src/cargo/core/manifest.rs | 35 ++++++++++------------- src/cargo/util/toml.rs | 57 ++++++++++++++++++++------------------ tests/metadata.rs | 4 +-- tests/read-manifest.rs | 18 ++++-------- 4 files changed, 53 insertions(+), 61 deletions(-) diff --git a/src/cargo/core/manifest.rs b/src/cargo/core/manifest.rs index d5a2f25e0..86cd6eebd 100644 --- a/src/cargo/core/manifest.rs +++ b/src/cargo/core/manifest.rs @@ -293,11 +293,12 @@ impl VirtualManifest { } impl Target { - fn blank() -> Target { + fn with_path(src_path: PathBuf) -> Target { + assert!(src_path.is_absolute()); Target { kind: TargetKind::Bin, name: String::new(), - src_path: PathBuf::new(), + src_path: src_path, doc: false, doctest: false, harness: true, @@ -309,67 +310,61 @@ impl Target { pub fn lib_target(name: &str, crate_targets: Vec, - src_path: &Path) -> Target { + src_path: PathBuf) -> Target { Target { kind: TargetKind::Lib(crate_targets), name: name.to_string(), - src_path: src_path.to_path_buf(), doctest: true, doc: true, - ..Target::blank() + ..Target::with_path(src_path) } } - pub fn bin_target(name: &str, src_path: &Path) -> Target { + pub fn bin_target(name: &str, src_path: PathBuf) -> Target { Target { kind: TargetKind::Bin, name: name.to_string(), - src_path: src_path.to_path_buf(), doc: true, - ..Target::blank() + ..Target::with_path(src_path) } } /// Builds a `Target` corresponding to the `build = "build.rs"` entry. - pub fn custom_build_target(name: &str, src_path: &Path) -> Target { + pub fn custom_build_target(name: &str, src_path: PathBuf) -> Target { Target { kind: TargetKind::CustomBuild, name: name.to_string(), - src_path: src_path.to_path_buf(), for_host: true, benched: false, tested: false, - ..Target::blank() + ..Target::with_path(src_path) } } - pub fn example_target(name: &str, src_path: &Path) -> Target { + pub fn example_target(name: &str, src_path: PathBuf) -> Target { Target { kind: TargetKind::Example, name: name.to_string(), - src_path: src_path.to_path_buf(), benched: false, - ..Target::blank() + ..Target::with_path(src_path) } } - pub fn test_target(name: &str, src_path: &Path) -> Target { + pub fn test_target(name: &str, src_path: PathBuf) -> Target { Target { kind: TargetKind::Test, name: name.to_string(), - src_path: src_path.to_path_buf(), benched: false, - ..Target::blank() + ..Target::with_path(src_path) } } - pub fn bench_target(name: &str, src_path: &Path) -> Target { + pub fn bench_target(name: &str, src_path: PathBuf) -> Target { Target { kind: TargetKind::Bench, name: name.to_string(), - src_path: src_path.to_path_buf(), tested: false, - ..Target::blank() + ..Target::with_path(src_path) } } diff --git a/src/cargo/util/toml.rs b/src/cargo/util/toml.rs index e670715d4..b708d18cf 100644 --- a/src/cargo/util/toml.rs +++ b/src/cargo/util/toml.rs @@ -29,6 +29,7 @@ pub struct Layout { examples: Vec, tests: Vec, benches: Vec, + } impl Layout { @@ -549,7 +550,8 @@ impl TomlManifest { let new_build = self.maybe_custom_build(&project.build, &layout.root, &mut warnings); // Get targets - let targets = normalize(&lib, + let targets = normalize(&layout.root, + &lib, &bins, new_build, &examples, @@ -1090,7 +1092,8 @@ impl fmt::Debug for PathValue { } } -fn normalize(lib: &Option, +fn normalize(package_root: &Path, + lib: &Option, bins: &[TomlBinTarget], custom_build: Option, examples: &[TomlExampleTarget], @@ -1110,7 +1113,7 @@ fn normalize(lib: &Option, }); } - fn lib_target(dst: &mut Vec, l: &TomlLibTarget) { + let lib_target = |dst: &mut Vec, l: &TomlLibTarget| { let path = l.path.clone().unwrap_or( PathValue::Path(Path::new("src").join(&format!("{}.rs", l.name()))) ); @@ -1124,71 +1127,71 @@ fn normalize(lib: &Option, }; let mut target = Target::lib_target(&l.name(), crate_types, - &path.to_path()); + package_root.join(path.to_path())); configure(l, &mut target); dst.push(target); - } + }; - fn bin_targets(dst: &mut Vec, bins: &[TomlBinTarget], - default: &mut FnMut(&TomlBinTarget) -> PathBuf) { + let bin_targets = |dst: &mut Vec, bins: &[TomlBinTarget], + default: &mut FnMut(&TomlBinTarget) -> PathBuf| { for bin in bins.iter() { let path = bin.path.clone().unwrap_or_else(|| { PathValue::Path(default(bin)) }); - let mut target = Target::bin_target(&bin.name(), &path.to_path()); + let mut target = Target::bin_target(&bin.name(), package_root.join(path.to_path())); configure(bin, &mut target); dst.push(target); } - } + }; - fn custom_build_target(dst: &mut Vec, cmd: &Path) { + let custom_build_target = |dst: &mut Vec, cmd: &Path| { let name = format!("build-script-{}", cmd.file_stem().and_then(|s| s.to_str()).unwrap_or("")); - dst.push(Target::custom_build_target(&name, cmd)); - } + dst.push(Target::custom_build_target(&name, package_root.join(cmd))); + }; - fn example_targets(dst: &mut Vec, - examples: &[TomlExampleTarget], - default: &mut FnMut(&TomlExampleTarget) -> PathBuf) { + let example_targets = |dst: &mut Vec, + examples: &[TomlExampleTarget], + default: &mut FnMut(&TomlExampleTarget) -> PathBuf| { for ex in examples.iter() { let path = ex.path.clone().unwrap_or_else(|| { PathValue::Path(default(ex)) }); - let mut target = Target::example_target(&ex.name(), &path.to_path()); + let mut target = Target::example_target(&ex.name(), package_root.join(path.to_path())); configure(ex, &mut target); dst.push(target); } - } + }; - fn test_targets(dst: &mut Vec, - tests: &[TomlTestTarget], - default: &mut FnMut(&TomlTestTarget) -> PathBuf) { + let test_targets = |dst: &mut Vec, + tests: &[TomlTestTarget], + default: &mut FnMut(&TomlTestTarget) -> PathBuf| { for test in tests.iter() { let path = test.path.clone().unwrap_or_else(|| { PathValue::Path(default(test)) }); - let mut target = Target::test_target(&test.name(), &path.to_path()); + let mut target = Target::test_target(&test.name(), package_root.join(path.to_path())); configure(test, &mut target); dst.push(target); } - } + }; - fn bench_targets(dst: &mut Vec, - benches: &[TomlBenchTarget], - default: &mut FnMut(&TomlBenchTarget) -> PathBuf) { + let bench_targets = |dst: &mut Vec, + benches: &[TomlBenchTarget], + default: &mut FnMut(&TomlBenchTarget) -> PathBuf| { for bench in benches.iter() { let path = bench.path.clone().unwrap_or_else(|| { PathValue::Path(default(bench)) }); - let mut target = Target::bench_target(&bench.name(), &path.to_path()); + let mut target = Target::bench_target(&bench.name(), package_root.join(path.to_path())); configure(bench, &mut target); dst.push(target); } - } + }; let mut ret = Vec::new(); diff --git a/tests/metadata.rs b/tests/metadata.rs index 846647123..a7b63bbce 100644 --- a/tests/metadata.rs +++ b/tests/metadata.rs @@ -27,7 +27,7 @@ fn cargo_metadata_simple() { "bin" ], "name": "foo", - "src_path": "src[/]foo.rs" + "src_path": "[..][/]foo[/]src[/]foo.rs" } ], "features": {}, @@ -341,7 +341,7 @@ const MANIFEST_OUTPUT: &'static str= "targets":[{ "kind":["bin"], "name":"foo", - "src_path":"src[/]foo.rs" + "src_path":"[..][/]foo[/]src[/]foo.rs" }], "features":{}, "manifest_path":"[..]Cargo.toml" diff --git a/tests/read-manifest.rs b/tests/read-manifest.rs index 44f7fa6f9..7aceb4545 100644 --- a/tests/read-manifest.rs +++ b/tests/read-manifest.rs @@ -4,12 +4,7 @@ extern crate hamcrest; use cargotest::support::{project, execs, main_file, basic_bin_manifest}; use hamcrest::{assert_that}; -fn remove_all_whitespace(s: &str) -> String { - s.split_whitespace().collect() -} - -fn read_manifest_output() -> String { - remove_all_whitespace(r#" +static MANIFEST_OUTPUT: &'static str = r#" { "name":"foo", "version":"0.5.0", @@ -21,12 +16,11 @@ fn read_manifest_output() -> String { "targets":[{ "kind":["bin"], "name":"foo", - "src_path":"src[..]foo.rs" + "src_path":"[..][/]foo[/]src[/]foo.rs" }], "features":{}, "manifest_path":"[..]Cargo.toml" -}"#) -} +}"#; #[test] fn cargo_read_manifest_path_to_cargo_toml_relative() { @@ -38,7 +32,7 @@ fn cargo_read_manifest_path_to_cargo_toml_relative() { .arg("--manifest-path").arg("foo/Cargo.toml") .cwd(p.root().parent().unwrap()), execs().with_status(0) - .with_stdout(read_manifest_output())); + .with_json(MANIFEST_OUTPUT)); } #[test] @@ -51,7 +45,7 @@ fn cargo_read_manifest_path_to_cargo_toml_absolute() { .arg("--manifest-path").arg(p.root().join("Cargo.toml")) .cwd(p.root().parent().unwrap()), execs().with_status(0) - .with_stdout(read_manifest_output())); + .with_json(MANIFEST_OUTPUT)); } #[test] @@ -91,5 +85,5 @@ fn cargo_read_manifest_cwd() { assert_that(p.cargo_process("read-manifest") .cwd(p.root()), execs().with_status(0) - .with_stdout(read_manifest_output())); + .with_json(MANIFEST_OUTPUT)); } -- 2.30.2